package com.lg.common.base;

import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.service.IService;
import com.lg.shiro.UserRealm;
import com.lg.system.user.entity.UserEntity;
import com.lg.system.user.service.UserRoleService;
import com.lg.system.user.service.UserService;
import com.lg.utils.R;

@Transactional
public  class BaseController<T,Service extends IService<T>>{
	
	
	@Autowired(required=false)
    private Service service;
	
	@Autowired
	UserRoleService userRoleService;
	
	@Autowired
	UserService userService;
	
	
	/** 
	* @Description: 操作公共方法
	* @param @param id
	* @param @return  
	* @return T 
	* @throws 
	*/ 
	@RequestMapping("selectById")
	@ResponseBody
	public T selectById(@RequestParam("id") Integer id){
		return service.selectById(id);
	}
	
	@RequestMapping("selectByIds")
	@ResponseBody
	public List<T> selectById(@RequestParam(value="ids[]",required=false) List<Integer> ids){
		return service.selectBatchIds(ids);
	}
	
	@RequiresRoles(logical=Logical.OR,value={"admin","base_delete"})
	@RequestMapping("deleteById")
	@ResponseBody
	public R deleteById(@RequestParam("id") Integer id){
		if(service.deleteById(id)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresRoles(logical=Logical.OR,value={"admin","base_delete"})
	@RequestMapping("deleteByIds")
	@ResponseBody
	public R deleteByIds(@RequestParam(value="ids[]",required=false) List<Integer> ids){
		if(service.deleteBatchIds(ids)){
			return R.ok();
		}else{
			return R.error();
		}
	}
	
	@RequiresRoles(logical=Logical.OR,value={"admin","base_delete"})
	@RequestMapping("deleteByMap")
	@ResponseBody
	public boolean deleteByMap(@RequestParam Map<String, Object> map){
		return service.deleteByMap(map);
	}
	
	
	@RequestMapping("insert")
	@ResponseBody
	public boolean insert(@RequestBody T t){
		return service.insert(t);
	}
	
	
	@RequestMapping("updateById")
	@ResponseBody
	public boolean updateById(@RequestBody T t){
		return service.updateById(t);
	}
	
	public  UserEntity getUser() {
		return (UserEntity)getSubjct().getPrincipal();
	}
	public  Long getUserId() {
		return getUser().getUserId();
	}
	
	public Subject getSubjct() {
		return SecurityUtils.getSubject();
	}

	/** 
	* @Description: 根据角色清除权限信息重新加载
	* @param   
	* @return void 
	* @throws 
	*/ 
	protected void clearAuthz(Long roleId){
		List<Long> userIds= userRoleService.selectUserIdByRId(roleId);
		List<UserEntity> users=userService.selectBatchIds(userIds);
	    Subject subject = getSubjct(); 
	    RealmSecurityManager rsm = (RealmSecurityManager)SecurityUtils.getSecurityManager();
	    UserRealm authRealm = (UserRealm)rsm.getRealms().iterator().next();
	    String realmName = subject.getPrincipals().getRealmNames().iterator().next(); 
        //第一个参数为用户名,第二个参数为realmName,user想要操作权限的用户 
	    for(UserEntity user:users){
	    	 SimplePrincipalCollection principals = new SimplePrincipalCollection(user,realmName); 
	         subject.runAs(principals); 
	         authRealm.getAuthorizationCache().remove(subject.getPrincipals()); 
	         subject.releaseRunAs();
	    }
	}
}